package com.app.micaihu.view.user.userinfo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.bean.comment.MyComment;
import com.app.micaihu.bean.comment.NewsComment;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.custom.view.MyGridView;
import com.app.micaihu.listener.CommentListener;
import com.app.micaihu.utils.CommentUtils;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.view.comment.SecondaryCommentDetailActivity;
import com.app.micaihu.view.main.topic.PostDetailActivity;
import com.app.micaihu.view.main.topic.adapter.SendImageGridAdapter;
import com.app.micaihu.view.newsdetail.ImageShowActivity;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.view.expression.listener.Clickable;
import com.app.utils.util.view.expression.view.BiaoQinTextView;
import com.blankj.utilcode.util.ActivityUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Guo on 2017/2/17.
 */

public class MyDynamicAdapter2052 extends AppBaseAdapter<MyComment> implements Clickable.ClickListener, AdapterView.OnItemClickListener {

    private CommentListener commentListener;
    private MyComment currentComment = null;
    private boolean isMine;


    public MyDynamicAdapter2052(List<MyComment> list, Context context, boolean isMine) {
        super(list, context);
        this.isMine = isMine;
    }


    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mycomment_2052, null);
            viewHolder = new MyDynamicAdapter2052.ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MyComment myComment = list.get(position);
        if (myComment == null) {
            convertView.setVisibility(View.GONE);
            return convertView;
        }
        if (convertView.getVisibility() == View.GONE) {
            convertView.setVisibility(View.VISIBLE);
        }
        convertView.setTag(R.id.tag_first, myComment);
        viewHolder.llArticleContent.setTag(myComment);
        viewHolder.tvPublish.setText(myComment.getPublishTime());
//        viewHolder.tvCommentContent.setPicText(myComment.getComment());
        viewHolder.container.setTag(R.id.tag_first, myComment);
        if (TextUtils.isEmpty(myComment.getThumbImg())) {
            viewHolder.civThumb.setVisibility(View.GONE);
        } else {
            viewHolder.civThumb.setVisibility(View.VISIBLE);
            LoadImageUtils.getInstance().loadImage(viewHolder.civThumb, myComment.getThumbImg());
        }


        if (TextUtils.equals(TypeConstant.POST_2052, myComment.getArticleType())) {//新版本的话题
            if (!TextUtils.isEmpty(myComment.getComment())) {
                viewHolder.tvCommentContent.setVisibility(View.VISIBLE);
                setClickableName(viewHolder.tvCommentContent, myComment);
            } else {
                viewHolder.tvCommentContent.setVisibility(View.GONE);
            }


        } else {
            viewHolder.tvCommentContent.setVisibility(View.VISIBLE);
            setClickableName(viewHolder.tvCommentContent, myComment);
        }
        viewHolder.tvArticleTitle.setPicText("[" + myComment.getTypeName() + "] " + myComment.getArticleTitle());


        if (isMine) {
            viewHolder.ivMenu.setVisibility(View.VISIBLE);
            viewHolder.ivMenu.setTag(myComment);
        } else {
            viewHolder.ivMenu.setVisibility(View.GONE);
        }


        List<String> contentThumbList = myComment.getContentThumbList();
        if (contentThumbList == null || contentThumbList.isEmpty()) {
            viewHolder.gridView.setVisibility(View.GONE);
        } else {
            viewHolder.gridView.setVisibility(View.VISIBLE);
            viewHolder.gridView.setOnItemClickListener(MyDynamicAdapter2052.this);
            viewHolder.gridView.setTag(contentThumbList);
            if (contentThumbList.size() > 3) {
                contentThumbList = contentThumbList.subList(0, 3);
            }

            if (viewHolder.gridView.getAdapter() == null) {
                SendImageGridAdapter sendImageGridAdapter = new SendImageGridAdapter(contentThumbList, context);
                viewHolder.gridView.setAdapter(sendImageGridAdapter);
            } else {
                ((SendImageGridAdapter) viewHolder.gridView.getAdapter()).setList(contentThumbList);
                ((SendImageGridAdapter) viewHolder.gridView.getAdapter()).notifyDataSetChanged();

            }


        }


        return convertView;
    }


    /**
     * 设置@的点击效果的方法
     *
     * @param myComment
     */
    private void setClickableName(BiaoQinTextView biaoqing, MyComment myComment) {
        String nickName = myComment.getpNickName();
        String uid = myComment.getpUid();
        ArrayList<String> strs = new ArrayList<>();
        ArrayList<Integer> color = new ArrayList<>();
        ArrayList<Object> parms = new ArrayList<>();
        String type = "@";
        //添加第一个参数
        if (!TextUtils.isEmpty(nickName) && !TextUtils.isEmpty(uid)) {
            strs.add(type + nickName + " ");
            color.add(Color.parseColor("#546a93"));
            parms.add(uid);
        }
        //添加第二个参数
        strs.add(myComment.getComment());
        //日间颜色
        color.add(Color.parseColor("#ff505050"));
        parms.add(myComment);
        biaoqing.setText(strs, color, parms, this);

    }

    @Override
    public void click(int position, ArrayList<Object> parms) {
        if (parms == null) {
            return;
        }
        if (parms.size() == 1) {
            MyComment myComment;
            try {
                myComment = (MyComment) parms.get(0);
            } catch (Exception e) {
                myComment = null;
            }
            goToCommentDetail(myComment);
        } else if (parms.size() == 2) {
            if (position == 0) {
                String uid = parms.get(0).toString();
                //点击评论用户昵称
                MyHomepageDynamicActivity.goToUserHomePage(ActivityUtils.getTopActivity(), uid);
            } else if (position == 1) {
                MyComment myComment;
                try {
                    myComment = (MyComment) parms.get(1);
                } catch (Exception e) {
                    myComment = null;
                }
                goToCommentDetail(myComment);
            }
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            ArrayList<String> thumbList = (ArrayList<String>) parent.getTag();
            Intent intent = new Intent(parent.getContext(), ImageShowActivity.class);
            intent.putStringArrayListExtra(Config.Extra.PARAMETER1, thumbList);
            intent.putExtra(Config.Extra.PARAMETER2, position);
            intent.setClass(parent.getContext(), ImageShowActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            parent.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    class ViewHolder implements View.OnClickListener {

        TextView tvPublish;//发布时间
        BiaoQinTextView tvCommentContent;//评论内容-带表情
        CustomImageView civThumb;//文章缩略图
        BiaoQinTextView tvArticleTitle;//文章title
        LinearLayout llArticleContent;//文章的容器
        ImageView ivMenu;//当个item的菜单键
        LinearLayout container;

        MyGridView gridView;

        private HashMap<String, Object> dialogParams;
        private View deleteView;

        public ViewHolder(View convertView) {
            tvPublish = (TextView) convertView.findViewById(R.id.comment_time);
            container = (LinearLayout) convertView.findViewById(R.id.container);
            tvCommentContent = (BiaoQinTextView) convertView.findViewById(R.id.comment_message);
            civThumb = (CustomImageView) convertView.findViewById(R.id.comment_news_img);
            tvArticleTitle = (BiaoQinTextView) convertView.findViewById(R.id.comment_news_title);
            llArticleContent = (LinearLayout) convertView.findViewById(R.id.comment_news_layout);
            ivMenu = (ImageView) convertView.findViewById(R.id.iv_menu);
            gridView = (MyGridView) convertView.findViewById(R.id.gridview);


            tvPublish.setOnClickListener(this);
            container.setOnClickListener(this);

            llArticleContent.setOnClickListener(this);
            convertView.setOnClickListener(this);
            ivMenu.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v == null) return;
            switch (v.getId()) {
                case R.id.iv_menu://点击item菜单
                    if (v.getTag() == null || !(v.getTag() instanceof MyComment)) return;
                    MyComment myComment = (MyComment) v.getTag();

                    showDelete(myComment);
                    break;

                case R.id.tv_cancel:
                    DialogUtils.getInstance().dismissCDialog();
                    break;
                case R.id.tv_save:
                    DialogUtils.getInstance().dismissCDialog();
                    requestNet2Delete();
                    break;

                case R.id.comment_news_layout://跳转到内页
                    if (v.getTag() == null || !(v.getTag() instanceof MyComment)) return;
                    MyComment tag = (MyComment) v.getTag();
                    NewsEntity newsEntity = new NewsEntity();
                    newsEntity.setArticleId(tag.getArticleId());
                    newsEntity.setArticleType(tag.getArticleType());
                    newsEntity.setGameId(tag.getGameId());

                    TypeConstant.openTypeDetail(newsEntity, (Activity) context, "");
                    break;
                case R.id.comment_parentlayout://跳转评论详情
                case R.id.container:
                    if (v.getTag() == null || !(v.getTag() instanceof MyComment)) return;
                    MyComment tag1 = (MyComment) v.getTag(R.id.tag_first);
                    if (tag1 == null) return;
                    goToCommentDetail(tag1);
                    break;
            }
        }

        private void showDelete(MyComment myComment) {
            currentComment = myComment;
            if (deleteView == null) {
                deleteView = LayoutInflater.from(context).inflate(R.layout.dialog_save_image, null);
                TextView cancel = (TextView) deleteView.findViewById(R.id.tv_cancel);
                TextView save = (TextView) deleteView.findViewById(R.id.tv_save);
                cancel.setText(AppApplication.getInstance().getString(R.string.common_cancel));
                cancel.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_main_textcolor));
                save.setText(AppApplication.getInstance().getString(R.string.common_delete));
                save.setTextColor(AppApplication.getInstance().getResources().getColor(R.color.common_red_color));
                cancel.setOnClickListener(this);
                save.setOnClickListener(this);
            }
            if (dialogParams == null) {
                dialogParams = new HashMap<>();
                dialogParams.put("anim", R.style.dialogWindowAnim);
                dialogParams.put("local", Gravity.BOTTOM);
            }
            DialogUtils.getInstance().showCDialog(deleteView, context, dialogParams);
        }


    }


    /**
     * 跳转到评论详情
     *
     * @param tag1
     */
    private void goToCommentDetail(MyComment tag1) {
        if (tag1 == null || context == null) return;
        Intent intent1 = new Intent();
        if (TypeConstant.POST_2052.equals(tag1.getArticleType())) {
            if (TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_PARENT)) {
                intent1.setClass(context, PostDetailActivity.class);
//                intent1.putExtra(BaseActivity.PARAMETER1, tag1.getArticleId());
                intent1.putExtra(Config.Extra.PARAMETER1, tag1.getCommentId());
            } else if (TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_TWO)
                    || TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_ONE)) {
                intent1.setClass(context, SecondaryCommentDetailActivity.class);
                intent1.putExtra(Config.Extra.PARAMETER1, tag1.getArticleId());
                intent1.putExtra(Config.Extra.PARAMETER2, tag1.getParentCmtId());
                intent1.putExtra(SecondaryCommentDetailActivity.COMMENT_OR_POST, false);
            }

        } else {
            if (TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_PARENT)) {
                intent1.putExtra(Config.Extra.PARAMETER2, tag1.getCommentId());
            } else if (TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_TWO)
                    || TextUtils.equals(tag1.getCmtType(), TypeConstant.COMMENT_ONE)) {
                intent1.putExtra(Config.Extra.PARAMETER2, tag1.getParentCmtId());
            }
            intent1.putExtra(SecondaryCommentDetailActivity.COMMENT_OR_POST, true);
            intent1.setClass(context, SecondaryCommentDetailActivity.class);
            intent1.putExtra(Config.Extra.PARAMETER1, tag1.getArticleId());

        }
        if (intent1.getComponent() != null) {
            context.startActivity(intent1);
        }
    }


    /**
     * 访问网络删除评论
     */
    private void requestNet2Delete() {
        if (currentComment == null) return;
        if (commentListener == null) {
            commentListener = new CommentListener() {
                @Override
                public void commentStart() {
                    DialogUtils.getInstance().showProgressDialog((Activity) context, AppApplication.getInstance().getResources().getString(R.string.homepage_attention_start));
                }

                @Override
                public void commentFail(VolleyError volley) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    ToastUtils.makeText(AppApplication.getInstance().getString(R.string.neterror));
                }

                @Override
                public void commentSuccess(NewsComment newsComment) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    ToastUtils.makeText(AppApplication.getInstance().getString(R.string.common_delete_suc));
                    list.remove(currentComment);
                    notifyDataSetChanged();
                    currentComment = null;
                }
            };
        }
        CommentUtils.getInstance().deleteComment(currentComment.getArticleId(), currentComment.getCommentId(), "0", commentListener, "");


    }
}
